package com.siruier.boss.router

import android.app.Activity
import android.content.Context
import android.content.Intent
import com.didi.drouter.api.DRouter
import com.didi.drouter.router.Request
import com.didi.drouter.router.RouterCallback.ActivityCallback

fun Context.startRouter(
    uri: String,
    ext: (Request.() -> Unit)? = null
) {
    startRouterResult(uri, ext)
}

fun Context.startRouterResult(
    uri: String,
    ext: (Request.() -> Unit)? = null,
    resultOk: Boolean = true,
    result: ((resultCode: Int, data: Intent?) -> Unit)? = null
) {
    DRouter.build(uri)
        .apply { ext?.let { also(it) } }
        .start(this, result?.let {
            object : ActivityCallback() {
                override fun onActivityResult(resultCode: Int, data: Intent?) {
                    if (resultOk) {
                        if (Activity.RESULT_OK == resultCode) {
                            it.invoke(resultCode, data)
                        }
                    } else {
                        it.invoke(resultCode, data)
                    }
                }
            }
        })
}

const val ROUTER_SCHEME = "tqy"
const val ROUTER_HOST = "local"
const val NEED_LOGIN = "needLogin"

const val ROUTER_PATH_LOGIN = "/login"
const val ROUTER_LOGIN = "${ROUTER_SCHEME}://${ROUTER_HOST}${ROUTER_PATH_LOGIN}"

const val ROUTER_PATH_ARTICLE_SEARCH = "/articleSearch"
const val ROUTER_ARTICLE_SEARCH = "${ROUTER_SCHEME}://${ROUTER_HOST}${ROUTER_PATH_ARTICLE_SEARCH}"

const val ROUTER_PATH_MY_PUBLISH = "/${NEED_LOGIN}/myPublish"
const val ROUTER_MY_PUBLISH = "${ROUTER_SCHEME}://${ROUTER_HOST}${ROUTER_PATH_MY_PUBLISH}"

const val ROUTER_PATH_PUBLISH_ADD = "/${NEED_LOGIN}/publishAdd"
const val ROUTER_PUBLISH_ADD = "${ROUTER_SCHEME}://${ROUTER_HOST}${ROUTER_PATH_PUBLISH_ADD}"

const val ROUTER_PATH_REPORT = "/report"
const val ROUTER_REPORT = "${ROUTER_SCHEME}://${ROUTER_HOST}${ROUTER_PATH_REPORT}"

const val ROUTER_PATH_VIP_OPEN = "/${NEED_LOGIN}/vipOpen"
const val ROUTER_VIP_OPEN = "${ROUTER_SCHEME}://${ROUTER_HOST}${ROUTER_PATH_VIP_OPEN}"

const val ROUTER_PATH_FORGET_PASSWORD = "/forgetPassword"
const val ROUTER_FORGET_PASSWORD = "${ROUTER_SCHEME}://${ROUTER_HOST}${ROUTER_PATH_FORGET_PASSWORD}"

const val ROUTER_PATH_CONTACT_US = "/contactUs"
const val ROUTER_CONTACT_US = "${ROUTER_SCHEME}://${ROUTER_HOST}${ROUTER_PATH_CONTACT_US}"

const val ROUTER_PATH_SERVICE_CONFIG = "/serviceConfig"
const val ROUTER_SERVICE_CONFIG = "${ROUTER_SCHEME}://${ROUTER_HOST}${ROUTER_PATH_SERVICE_CONFIG}"

const val ROUTER_PATH_SETTING = "/setting"
const val ROUTER_SETTING = "${ROUTER_SCHEME}://${ROUTER_HOST}${ROUTER_PATH_SETTING}"

const val ROUTER_PATH_WEB = "/web"
const val ROUTER_WEB = "${ROUTER_SCHEME}://${ROUTER_HOST}${ROUTER_PATH_WEB}"